<?php 
/*
 * 分站的处理
 * @package    city
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 0676eb058c1b2ad6c3ae140657c1bf6edd227c52 $
 */
class City {
	private static $_table;

	private static function _getTable() {
		if (!empty(self::$_table)) {
			return self::$_table;
		} else {
			self::$_table = Table::name(Table::NAME_CITY);
			return self::$_table;
		}
	}

	/**
	 * 根据字母索引获取分站信息
	 * @return array
	 */
	public static function listsByABC() {
		global $db;
		$table = self::_getTable();
		$lists = array();
		$result = $db->query("SELECT areaid,name,style,domain,letter FROM {$table} ORDER BY letter,listorder");
		while($r = $db->fetchArray($result)) {
			$r['linkurl'] = $r['domain'] ? $r['domain'] : '';
			$lists[strtoupper($r['letter'])][] = $r;
		}
		return $lists;
	}

	public static function listsByArea(){
		global $db, $AREA;
		$AREA = !empty($AREA) ? $AREA : PCache::read('area.php');
		$table = self::_getTable();
		$lists = array();
		$sql = "select areaid, name from {$table} order by listorder DESC";
		$result = $db->query($sql);
		while($r = $db->fetchArray($result)) {
			$area = $AREA[$r['areaid']];
			if ($r['areaid']) {
				if (!isset($lists[$area['parentid']])) {
					$lists[$area['parentid']] = $AREA[$area['parentid']];
				}
				$lists[$area['parentid']]['subArea'][] = $r;
			} else {
				continue;
			}
		}
		return $lists;
	}

	public static function getCitys($num = 10){
		global $db;
		$table = self::_getTable();
		$sql = "select areaid, name from {$table} order by areaid ASC limit {$num}";
		$lists = $db->getAll($sql);
		return $lists;
	}

	/**
	 * 是否是城市分站
	 * @param $cityid
	 * @return bool
	 */
	public static function isCity($cityid = 0){
		global $db;
		$table = self::_getTable();
		$sql = "select areaid, name from {$table} where areaid=$cityid";
		$lists = $db->getAll($sql);
		if($lists){
			return true;
		}else{
			return false;
		}
	}
}